package com.easy.reconciliation.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Packge com.easy.reconciliation.controller
 * @Autor linNaibin
 * @Description
 * @Version 1.0
 * @Date 2021/11/18 11:44
 */
@RestController
public class FollowerController3 {


    private static final Logger logger = LoggerFactory.getLogger(FollowerController3.class);


    @GetMapping("/heartbeat3")
    public JSONObject heartbeat() {
        logger.info("服务器3接收到leader的心跳.");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        return jsonObject;
    }

    @GetMapping("/execute3")
    public JSONObject execute() {
        logger.info("服务器3 开始处理 leader 的命令...");

        try {
            // 休眠
            Random random = new Random();
            TimeUnit.SECONDS.sleep(random.nextInt(5) + 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("服务器3已经处理完毕...");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        return jsonObject;
    }
}
